package pho.helper;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author Peter
 *
 * an implementation of the FileNameFilter that can filter filenames with a
 * list of suffixes like .jpg | .jpeg etc. This cannot match wildcards like *.*
 */
public class SuffixFileNameFilter implements FilenameFilter 
{
	private List suffixList;

	/**
	 * creates the file name filter with a string containing one or more suffixes
	 * separated by | (pipe)
	 * @param suffixes The suffixes to be accepted by the filter
	 */
	public SuffixFileNameFilter (String suffixes)
	{		
		StringTokenizer st = new StringTokenizer (suffixes,"|");
		suffixList = new ArrayList();
		while (st.hasMoreTokens()) 
		{
        	suffixList.add(st.nextToken().trim());
     	}

	}

	/*
	 * @see java.io.FilenameFilter#accept(File, String)
	 */
	public boolean accept(File dir, String name) 
	{
		Iterator iterator = suffixList.iterator();
		while (iterator.hasNext())
		{
			if (name.toUpperCase().endsWith (((String)iterator.next()).toUpperCase()))
			{
				return true;
			}
		}
		return false;
	}
}
